﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Silvia.Models.Home;

namespace Silvia.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/


        public ActionResult Index()
        {
            IndexModel model = new IndexModel();
            ORDesignerDataContext entities = new ORDesignerDataContext();
            var element = (from u in entities.PageElement
                           where u.Type == "Contact"
                           select u).SingleOrDefault();
            if (element != null)
                model.Contact = element.Content;
            else
                model.Contact = "";

            model.ImageList = (from u in entities.PageElement
                               where u.Type == "HomePhoto"
                               orderby u.Seq
                               select u).ToList();

            loadLayout(model, entities);

            return View(model);
        }

        private static void loadLayout(LayoutModel model, ORDesignerDataContext entities)
        {
            model.AlbumList = (from a in entities.Album
                               where a.ParentID == null
                               orderby a.Seq
                               select a).ToList();

            var element = (from u in entities.SystemData
                           where u.Name == "Footer"
                           select u).SingleOrDefault();
            if (element != null)
            {
                model.Footer = element.Value;
            }
        }

        public ActionResult Team()
        {
            TeamModel model = new TeamModel();
            ORDesignerDataContext entities = new ORDesignerDataContext();
            model.List = (from m in entities.Member
                          orderby m.Seq
                          select m).ToList();
            loadLayout(model, entities);
            return View(model);
        }

        public ActionResult Album(int id)
        {
            AlbumModel model = new AlbumModel();
            ORDesignerDataContext entities = new ORDesignerDataContext();
            var album = (from a in entities.Album
                         where a.Id == id
                         select a).SingleOrDefault();
            model.Name = album.Name;
            model.ID = album.Id;
            model.List = (from a in entities.Album
                          orderby a.Seq
                          where a.ParentID == id
                          select new UIAlbum()
                          {
                              ID = a.Id,
                              Name = a.Name
                          }).ToList();

            foreach (var item in model.List)
            {
                item.List = (from p in entities.Photo
                             where p.AlbumID == item.ID
                             orderby p.Seq
                             select p).ToList();
            }
            loadLayout(model, entities);
            return View(model);
        }

        public ActionResult PriceBook()
        {
            PriceBookModel model = new PriceBookModel();
            ORDesignerDataContext entities = new ORDesignerDataContext();
            var element = (from u in entities.PageElement
                           where u.Type == "PriceBook"
                           select u).SingleOrDefault();
            if (element != null)
                model.Text = element.Content;
            else
                model.Text = "";
            loadLayout(model, entities);
            return View(model);
        }
    }
}
